package com.fiap.activities;

import java.util.Calendar;

import org.json.JSONException;
import org.json.JSONObject;

import com.fiap.dao.UserOperations;
import com.fiap.functions.UserFunctions;
import com.fiap.model.User;
import com.fiap.sqlite.DatabaseHelper;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class InitialInfoActivity extends Activity {
	
	private Button btnSkipStep;
    private Spinner spinnerSex;
    private EditText txtWeight;
    private EditText txtHeight;
	private Button btnSaveInfo;
	//private DatePicker dpBirthday;
	private TextView tvDisplayDate;
	private Button btnChangeDate;
	private TextView initialInfoErrorMsg;
	private int year;
	private int month;
	private int day;
	
	static final int DATE_DIALOG_ID = 999;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
//    	requestWindowFeature(Window.FEATURE_NO_TITLE);
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.initial_info);
//        
//        boolean firstTime = getIntent().getBooleanExtra("firstTime", false);
//        
//        prepareScreen();
//        
//        if(firstTime){
//        	//Show button to skip the step
//        	btnSkipStep.setVisibility(0);
//        }
//        
//        
//        setCurrentDateOnView();
//        addListenerOnButton();
//        
//        // Listening to Login Screen link
//        btnSkipStep.setOnClickListener(new View.OnClickListener() {
//			
//			public void onClick(View arg0) {
//				Intent i = new Intent(getApplicationContext(), MainMenuActivity.class);
//                // Close all views before launching
//                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//                startActivity(i);
//                finish();
//			}
//		});
//        
//        btnSaveInfo.setOnClickListener(new View.OnClickListener() {
//			
//			public void onClick(View arg0) {
//				
//				String sex = spinnerSex.getSelectedItem().toString();
//                double weight = Double.parseDouble(txtWeight.getText().toString());
//                double height = Double.parseDouble(txtHeight.getText().toString());
//                String dateBirthday = tvDisplayDate.getText().toString();
//                
//                UserFunctions userFunction = new UserFunctions();
//                User user = userFunction.getUser(getApplicationContext());
//
//                JSONObject json = userFunction.updateInitialInfo(sex, weight, height, dateBirthday, 
//                		user.getEmail(), user.getId());
//                
//                try {
//                    if (json.getString("success") != null) {
//                        initialInfoErrorMsg.setText("");
//                        String res = json.getString("success"); 
//                        if(Integer.parseInt(res) == 1){
//                            // user successfully registred
//                            UserOperations userOperations = new UserOperations(getApplicationContext());
//                            JSONObject jsonUser = json.getJSONObject("user");
////                            userOperations.updateUserInitialInfo(jsonUser.getString("weight"), jsonUser.getString("height"), 
////                            		user.getId());
//                            
//                            Intent i = new Intent(getApplicationContext(), MainMenuActivity.class);
//                            startActivity(i);
//                            
//                        }else{
//                        	String errorMessage = json.getString("error_msg");
//                            // Error in registration, show error message
//                        	initialInfoErrorMsg.setText(errorMessage);
//                        	initialInfoErrorMsg.setVisibility(0);
//                        }
//                    }
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
//				
//			}
//		});
        
    }
    
	private void prepareScreen() {
		btnSkipStep = (Button) findViewById(R.id.link_skip_step);
        spinnerSex = (Spinner) findViewById(R.id.spinner_sex);
        txtWeight = (EditText) findViewById(R.id.txt_weight);
        txtHeight = (EditText) findViewById(R.id.txt_height);
        //dpBirthday = (DatePicker) findViewById(R.id.datepicker_birthday);
        btnSaveInfo = (Button) findViewById(R.id.btnSaveInfo);
        initialInfoErrorMsg = (TextView) findViewById(R.id.initial_info_error_msg);
	}
	
	public void setCurrentDateOnView() {
		 
		tvDisplayDate = (TextView) findViewById(R.id.tvDate);
		//dpBirthday = (DatePicker) findViewById(R.id.datepicker_birthday);
 
		final Calendar c = Calendar.getInstance();
		year = c.get(Calendar.YEAR);
		month = c.get(Calendar.MONTH);
		day = c.get(Calendar.DAY_OF_MONTH);
 
		// set current date into textview
		String dayZero = "";
		String monthZero = "";
		if(day < 10) {
			dayZero = "0";
		}
		if((month+1) < 10) {
			monthZero = "0";
		}
		tvDisplayDate.setText(new StringBuilder()
			// Month is 0 based, just add 1
			.append(dayZero)
			.append(day).append("/")
			.append(monthZero)
			.append(month + 1).append("/")
			.append(year).append(" "));
 
		// set current date into datepicker
		//dpBirthday.init(year, month, day, null);
 
	}
	
	public void addListenerOnButton() {
		 
		btnChangeDate = (Button) findViewById(R.id.btnChangeDate);
 
		btnChangeDate.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showDialog(DATE_DIALOG_ID);
			}
		});
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DATE_DIALOG_ID:
		   // set date picker as current date
		   return new DatePickerDialog(this, datePickerListener, 
                         year, month,day);
		}
		return null;
	}
	
	private DatePickerDialog.OnDateSetListener datePickerListener = 
			new DatePickerDialog.OnDateSetListener() {

		// when dialog box is closed, below method will be called.
		public void onDateSet(DatePicker view, int selectedYear,
				int selectedMonth, int selectedDay) {
			year = selectedYear;
			month = selectedMonth;
			day = selectedDay;
			
			String dayZero = "";
			String monthZero = "";
			if(day < 10) {
				dayZero = "0";
			}
			if((month+1) < 10) {
				monthZero = "0";
			}
			// set selected date into textview
			tvDisplayDate.setText(new StringBuilder()
				.append(dayZero)
				.append(day).append("/")
				.append(monthZero)
				.append(month + 1).append("/")
				.append(year).append(" "));

			// set selected date into datepicker also
			//dpBirthday.init(year, month, day, null);

		}
	};
	
	public void saveInfo(View v) {
    	Intent i = new Intent(getApplicationContext(), MainMenuActivity.class);
		startActivity(i);
		finish();
    }
    
}